#include <QFileInfo>
#include <QDir>
#include <QDebug>

#include "defines.h"
#include "generatordata.h"

#include "symbian/symbiandefines.h" // TODO remove eventually
#include "maemo5/maemo5defines.h" // TODO remove eventually

GeneratorData::GeneratorData()
{
    m_isRemotelyCompiled = false;
}

void GeneratorData::clearAll()
{
    m_configXmlData.clear();
    m_infoPlistData.clear();
    m_widgetSrcFiles.clear();
    m_pluginSrcFiles.clear();
    m_environmentVariables.clear();
    m_basePath.clear();
}

QString GeneratorData::infoPlistValue(const QString &key) const
{
    return m_infoPlistData.value(key);
}


QString GeneratorData::configXmlValue(const QString &key) const
{
    return m_configXmlData.value(key);
}

void GeneratorData::initPaths(QString projectFile,
                              QString platformName)
{
    QFileInfo targetInfo(projectFile);
    QString fileName = targetInfo.fileName().replace(QRegExp("\\s"), "_");

    m_buildDirectory = QString("/hag_build_");
    m_buildDirectory += platformName;
    m_buildDirectory += "/";

    /* OBS
    m_projectDirectory = QString("/hag_build/%1/").arg(fileName);
    m_widgetDirectory = QString("/hag_build/%1/widget/").arg(fileName);
    m_fwDirectory = QString("/hag_build/%1/fw/").arg(fileName);
    m_mwDirectory = QString("/hag_build/%1/mw/").arg(fileName);
    */

    m_projectDirectory = m_buildDirectory + QString("%1/").arg(fileName);



    if (platformName == SYMBIAN_PLATFORMNAME)
    {
        m_srcDirectory = m_projectDirectory;
        m_widgetDirectory = m_projectDirectory + QString("widget/");
    }
    else if (platformName == MAEMO5_PLATFORMNAME)
    {
        m_srcDirectory = m_projectDirectory + QString("src/");
        m_widgetDirectory = m_srcDirectory + QString("widget/");
    }
    else
    {
        ; // TODO this kind of code should not be here
        // TODO GeneratorData eventually should also be platform
        // specific
    }

    m_fwDirectory = m_projectDirectory + QString("fw/");
    m_mwDirectory = m_projectDirectory + QString("mw/");

    m_m5DataDirectory = m_projectDirectory + QString("data/");
    m_m5DebianDirectory = m_projectDirectory + QString("debian/");

    m_widgetFileName = fileName;

    if (targetInfo.canonicalPath().contains(QRegExp("\\s")))
    {
        // Widget path contains spaces -- use temp dir to make abld work
        m_basePath = QDir::tempPath() + HAG_TEMP_DIR;
    }
    else
    {
        // No spaces, use widget path
        m_basePath = targetInfo.canonicalPath();
    }
}

QString GeneratorData::basePath() const
{
    return m_basePath;
}

QString GeneratorData::widgetFileName() const
{
    return m_widgetFileName;
}

QString GeneratorData::buildDirectory() const
{
    return m_basePath + m_buildDirectory;
}

QString GeneratorData::projectDirectory() const
{
    return m_basePath + m_projectDirectory;
}

QString GeneratorData::widgetDirectory() const
{
    return m_basePath + m_widgetDirectory;
}

QString GeneratorData::fwDirectory() const
{
    return m_basePath + m_fwDirectory;
}

QString GeneratorData::mwDirectory() const
{
    return m_basePath + m_mwDirectory;
}

QString GeneratorData::srcDirectory() const
{
    return m_basePath + m_srcDirectory;
}

QString GeneratorData::m5DataDirectory() const
{
    return m_basePath + m_m5DataDirectory;
}

QString GeneratorData::m5DebianDirectory() const
{
    return m_basePath + m_m5DebianDirectory;
}

bool GeneratorData::isRemotelyCompiled() const
{
    return m_isRemotelyCompiled;
}

void GeneratorData::setRemotelyCompiled(bool remotelyCompiled)
{
    m_isRemotelyCompiled = remotelyCompiled;
}
